package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabFeeItem;
import com.ysd.lis.entity.lab.LabFeeitemVsBarcode;
import com.ysd.lis.entity.lab.LabInspectCompanyFeeitem;
import com.ysd.lis.mapper.lab.LabFeeItemMapper;
import com.ysd.lis.mapper.lab.LabInspectCompanyFeeitemMapper;
import com.ysd.lis.request.lab.CompFeeDto;
import com.ysd.lis.request.lab.CompanyPageParam;
import com.ysd.lis.service.lab.LabInspectCompanyFeeitemService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 通道号管理表 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-10-12
 */
@Service
public class LabInspectCompanyFeeitemServiceImpl extends ServiceImpl<LabInspectCompanyFeeitemMapper, LabInspectCompanyFeeitem> implements LabInspectCompanyFeeitemService {

    @Autowired
    LabInspectCompanyFeeitemMapper labInspectCompanyFeeitemMapper;
    @Autowired
    LabFeeItemMapper labFeeItemMapper;
    @Override
    public Result findInspectCompanyFeeitemList(CompanyPageParam param) {
        MPJLambdaWrapper<LabInspectCompanyFeeitem> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LabInspectCompanyFeeitem.class);
        queryWrapper.selectAs("fe", LabFeeItem::getFeeName, LabInspectCompanyFeeitem::getFeeName);

        queryWrapper.leftJoin(LabFeeItem.class, "fe", p -> p.eq(LabFeeItem::getFeeCode, LabInspectCompanyFeeitem::getFeeCode).eq(LabFeeItem::getDelFlag, 0));
        queryWrapper.eq(LabInspectCompanyFeeitem::getDelFlag, 0);
        queryWrapper.eq(LabInspectCompanyFeeitem::getCompanyCode,param.getCompanyCode());
        if(ToolsUtils.isNotEmpty(param.getSeachValue())){
            queryWrapper.and(wrapper ->
                    wrapper.like(LabInspectCompanyFeeitem::getFeeCode, param.getSeachValue())
                            .or()
                            .like(LabInspectCompanyFeeitem::getCompanyCode, param.getSeachValue())
            );
        }
        queryWrapper.orderByAsc(LabInspectCompanyFeeitem::getSeq);
        List<LabInspectCompanyFeeitem> detailList = labInspectCompanyFeeitemMapper.selectList(queryWrapper);

        return Result.succ(1, "查询成功", detailList);
    }

    @Override
    public Result addCompanyFeeitem(LabInspectCompanyFeeitem companyFeeitem) {

        int insert = labInspectCompanyFeeitemMapper.insert(companyFeeitem);
        if (insert > 0) {
            return Result.succ(1, "添加成功", companyFeeitem);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }

    @Override
    public Result saveOrUpdateCompFeeitem(CompFeeDto dto) {
        List<LabInspectCompanyFeeitem> list = dto.getInspectCompanyFeeitemList();
        boolean b = this.updateBatchById(list);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result batchDeleteCompanyFeeList(CompFeeDto dto) {
        List<LabInspectCompanyFeeitem> list = dto.getInspectCompanyFeeitemList();
        List<String> ids = list.stream().map(LabInspectCompanyFeeitem::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);

        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result findFeeitemListByBarCodeAndInspectCompany(CompanyPageParam param) {
        String companyCode = param.getCompanyCode();
        String barcodeNo = param.getBarcodeNo();
        MPJLambdaWrapper<LabFeeItem> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .selectAll(LabFeeItem.class)
                 .selectAs("lic", LabInspectCompanyFeeitem::getPrice, LabFeeItem::getPrice)
                .innerJoin(LabFeeitemVsBarcode.class, p->p.eq(LabFeeitemVsBarcode::getFeeCode, LabFeeItem::getFeeCode).eq(LabFeeitemVsBarcode::getBarcodeNo, barcodeNo).eq(LabFeeitemVsBarcode::getDelFlag, 0));

        if(ToolsUtils.isNotEmpty(companyCode)){
            mpjLambdaWrapper.innerJoin(LabInspectCompanyFeeitem.class,"lic", p->p.eq(LabInspectCompanyFeeitem::getFeeCode, LabFeeItem::getFeeCode).eq(LabInspectCompanyFeeitem::getCompanyCode,companyCode).eq(LabInspectCompanyFeeitem::getDelFlag,0));

        }
        mpjLambdaWrapper.eq(LabFeeItem::getDelFlag, 0)
                .orderByAsc(LabFeeItem::getSeq);
        List<LabFeeItem> labFeeItems = labFeeItemMapper.selectList(mpjLambdaWrapper);
        return Result.succ(1, "查询成功", labFeeItems);

    }
}
